const numberFormat = (format, value, fixType = 'round') => {
  value = Number(value)
  value = isNaN(value) ? 0 : value
  let endMark = ''
  const isComma = format[0] === '$' // 是否需要千分位
  const formatDotLength = (format.split('.')[1] || '').replace(/[^\d]/g, '').length // 小数长度
  let endMarkDotLength = 0
  if (format.indexOf('%') > -1) {
    endMark = '%'
    endMarkDotLength = 2
  }
  if (format.indexOf('‰') > -1) {
    endMark = '‰'
    endMarkDotLength = 3
  }
  if (format.indexOf('‱') > -1) {
    endMark = '‱'
    endMarkDotLength = 4
  }
  value = Math.mul(value, Math.pow(10, formatDotLength + endMarkDotLength))
  if (fixType === 'round') value = Number(value).toFixed(0)
  if (fixType === 'floor') value = Math.floor(Number(value))
  if (fixType === 'ceil') value = Math.ceil(Number(value))
  value = Math.div(value, Math.pow(10, formatDotLength), formatDotLength)
  // 小数点前面的值判断是否需要千分位隔开
  if (isComma) {
    const [int,
      smallNum] = value.split('.')
    value = `${int.replace(/\d{1,3}(?=(\d{3})+(\.\d*)?$)/g, '$&,')}${smallNum ? '.' + smallNum : ''}`
  }
  return value + endMark
}

const dateAndTimeFormatter = (row, column, timeProp) => {
  let date = row[column.property] // 日期
  if (!date) return '-'
  date = date.toString()
  if (date.length !== 8) return '-'
  const dateString = [
    date.slice(0, 4),
    date.slice(4, 6),
    date.slice(6, 8)]
    .join('-')
  let time = row[timeProp] // 时间
  let timeString
  if (!time) {
    timeString = ''
  } else {
    time = time.toString()
    const diff = 6 - time.length
    if (diff > 0) {
      time = '0'.repeat(diff) + time
    }
    timeString = [
      time.slice(0, 2),
      time.slice(2, 4),
      time.slice(4, 6)].join(':')
  }
  return (dateString + ' ' + timeString).trim()
}

const formatters = {
  // 时间格式化
  timeFormatter(row, column) {
    let time = row[column.property]
    if (!time) return '00:00:00'
    time = time.toString()
    const diff = 6 - time.length
    if (diff > 0) {
      time = '0'.repeat(diff) + time
    }
    return [time.slice(0, 2),
      time.slice(2, 4),
      time.slice(4, 6)]
      .join(':')
  },
  // 日期格式处理
  dateFormatter(row, column) {
    let date = row[column.property]
    if (!date) return '-'
    date = date.toString()
    if (date.length !== 8) return '-'
    return [date.slice(0, 4),
      date.slice(4, 6),
      date.slice(6, 8)]
      .join('-')
  },
  // 月份处理
  monthFormatter(row, column) {
    let date = row[column.property]
    if (!date) return '-'
    date = date.toString()
    if (date.length !== 6) return '-'
    return [date.slice(0, 4),
      date.slice(4, 6)
    ].join('-')
  },
  // 格式化为百分数 * 100
  percentFormatter(row, column) {
    const num = row[column.property]
    if (!num && ![0,
      '0'].includes(num)) return '-'
    return numberFormat('$0.00%', num)
  },
  // 格式化 日期+时间
  dateAndTimeFormatter(row, column) {
    // 将前缀一样的属性 替换为time 如updateDate = > updateTime 从而得到时间字段
    const timeProp = column.property.replace('Date', 'Time') // 时间字段
    const date = row[column.property] // 日期字段的值
    const time = row[timeProp] // 时间字段的值
    if (!date || !time) {
      return '-'
    }
    return dateAndTimeFormatter(row, column, timeProp)
  },
  // 格式化为万元 + 千分号
  wanAndThousandFormatter(row, column) {
    let num = row[column.property]
    if (!num && ![0,
      '0'].includes(num)) return '-'
    num = num / 10000
    return numberFormat('$0.00', num)
  },
  // 金额格式化 保留四位
  netValFormatter(row, column) {
    const num = row[column.property]
    if (!num && ![0,
      '0'].includes(num)) return '-'
    return numberFormat('0.0000', num)
  },
  // 金额类格式化 千位逗号分割(保留2位小数 可去除)
  AmtFormatter(row, column) {
    const num = row[column.property]
    if (!num && ![0,
      '0'].includes(num)) return '-'
    return numberFormat('$0.00', num)
  }
}
export default formatters
